﻿using AntMovement.Core;
using AntMovement.Core.Model;
using AntMovement.WPF.Resources.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Domain
{
    public class AntMovementLog : ILog
    {
        private AsyncObservableCollection<LogInfoModel> _infos { get; set; }
        private AsyncObservableCollection<LogInfoModel> _error { get; set; }
        public AntMovementLog()
        {
            _infos = new AsyncObservableCollection<LogInfoModel>();
            _error = new AsyncObservableCollection<LogInfoModel>();
        }

        public void Debug(string message) => AddInfo(message, "调试");

        public void Error(string type, string message)
        {
            try
            {
                _error.Insert(0, new LogInfoModel()
                {
                    InfoType = type,
                    Message = message,
                    Time = DateTime.Now.ToString("HH:mm:ss:fff"),
                });
                if (_error.Count > 800)
                {
                    _error.RemoveAt(800);
                }
            }
            catch (Exception)
            {
            }
        }

        public void Info(string message) => AddInfo(message, "信息");

        public void Warning(string message) => AddInfo(message, "警告");

        private void AddInfo(string message, string infotype)
        {
            try
            {
                _infos.Insert(0, new LogInfoModel()
                {
                    Message = message,
                    InfoType = infotype,
                    Time = DateTime.Now.ToString("HH:mm:ss:fff"),
                });
                if (_infos.Count > 800)
                {
                    _infos.RemoveAt(800);
                }
            }
            catch (Exception)
            {
            }
        }

        public IEnumerable<LogInfoModel> GetInfo() => _infos;

        public IEnumerable<LogInfoModel> GetError() => _error;

        public void ResettingError()
        {
            if (_error.Count > 0)
            {
                _error.Clear();
            }
        }
    }
}
